$().ready(function () {
    validateRule();
});

$.validator.setDefaults({
    submitHandler: function () {
        update();
    }
});

function update() {
    $.ajax({
        cache: true,
        type: "POST",
        url: "admin/dept/update?_method=put",
        data: $('#signupForm').serialize(),// 你的formid
        async: false,
        error: function (request) {
            parent.layer.alert("Connection error");
        },
        success: function (data) {
            if (data.code === 200) {
                parent.layer.msg("操作成功");
                parent.reLoad();
                var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
                parent.layer.close(index);

            } else {
                parent.layer.alert(data.msg)
            }

        }
    });

}

function validateRule() {
    var icon = "<i class='fa fa-times-circle'></i> ";
    $("#signupForm").validate({
        rules: {
            name: {
                required: true
            }
        },
        messages: {
            name: {
                required: icon + "请输入部门名称"
            }
        }
    })
}

var openDept = function (deptId) {
    layer.open({
        type: 2,
        title: "选择部门",
        area: ['300px', '450px'],
        content: "admin/dept/treeView?deptId=" + deptId
    })
};

function loadDept(pId, deptName, index, deptId) {
    if (pId != '-1') {
        $.get("admin/dept/isChild?deptId=" + deptId + "&pId=" + pId, function (data) {
            if (data) {
                layer.msg("不能选择当前部门或其子部门！")
            } else {
                $("#pId").val(pId);
                $("#deptName").val(deptName);
                layer.close(index)
            }
        });
    } else {
        $("#pId").val(0);
        $("#deptName").val('无');
        layer.close(index)
    }
}